package com.treasure.service.commonutil;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.ImageOutputStream;

import com.treasure.service.commons.lang.util.StringUtil;

/**
 * @ClassName : DrawingUtils
 * @author : panxingwu
 * @date : 2018年11月18日 下午8:20:16
 * @Description : 绘图工具
 */
public class DrawingUtils {
	private static float jPEGcompression = 1f;// 图片清晰比率
	private static final String INVITE_CARD_IMG = "http://raw.jingyugg.com/invite_card_sharepage-1.png";
	private static final String INVITE_VIP_IMG = "http://raw.jingyugg.com/invite_vip_sharepage-2.png";
	private static final String INVITE_LOANS_IMG = "http://raw.jingyugg.com/invite_loans_sharepage-2.png";


	/**
	 * @Description : 获取推广信用卡的海报
	 * @Param : originalImg 原图
	 * @Param : qrCodeLink 二维码链接地址
	 * @Param : shareDesc 图片文字
	 * @return : java.lang.String
	 */
	public static String getInviteCardImg(String qrCodeLink,BufferedImage imageCode, String shareDesc,String inviteCode) throws Exception {
		// 加载原图图片
		BufferedImage temp = ImageIO.read(new URL(INVITE_CARD_IMG));
		//解决图片失真的问题
		BufferedImage imageLocal = new BufferedImage(temp.getWidth(), temp.getHeight(), BufferedImage.TYPE_3BYTE_BGR);  
		imageLocal.getGraphics().drawImage(temp, 0, 0, null);  
		// 生成二维码
//		BufferedImage imageCode = QrCodeUtil.createQrCodeBuffer(qrCodeLink, "temp.jpg");
		// 以原图片为模板
		Graphics2D g = imageLocal.createGraphics();
		AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
		g.setComposite(ac);
		g.setBackground(Color.WHITE);
		// 在模板上添加用户二维码(地址,左边距,上边距,图片宽度,图片高度,未知)
		g.drawImage(imageCode, 419, 709, 124, 124, null);
		if (StringUtil.isNotEmpty(shareDesc)) {
			// 设置文本样式
			g.setFont(new Font("微软雅黑", Font.PLAIN, 40));
			g.setColor(Color.red);
			// 计算文字长度，计算居中的x点坐标
			g.drawString(shareDesc, imageLocal.getWidth() - 330, imageLocal.getHeight() - 530);

			// 设置文本样式
			g.setFont(new Font("微软雅黑", Font.PLAIN + Font.BOLD, 16));
			g.setColor(Color.WHITE);
			// 计算文字长度，计算居中的x点坐标
			String caewm = "长按二维码";
			g.drawString(caewm, 105, imageLocal.getHeight() - 10);
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		saveAsJPEG(imageLocal, out);
		//上传到七牛云并返回文件名称
		return QiNiuYun.uploadBytes("usershareimg",inviteCode+"_invitecard.jpg",out.toByteArray());
	}

	/**
	 * @Description : 获取邀请会员的海报
	 * @Param : originalImg 原图
	 * @Param : qrCodeLink 二维码链接地址
	 * @Param : shareDesc 图片文字
	 * @return : java.lang.String
	 */
	public static String getInviteVipImg(String qrCodeLink, String shareDesc,String inviteCode) throws Exception {
		// 加载原图图片
		BufferedImage temp = ImageIO.read(new URL(INVITE_VIP_IMG));
		//解决图片失真的问题
		BufferedImage imageLocal = new BufferedImage(temp.getWidth(), temp.getHeight(), BufferedImage.TYPE_3BYTE_BGR);  
		imageLocal.getGraphics().drawImage(temp, 0, 0, null);  
		// 生成二维码
		BufferedImage imageCode = QrCodeUtil.createQrCodeBuffer(qrCodeLink, "temp.jpg");
		// 以原图片为模板
		Graphics2D g = imageLocal.createGraphics();
		AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
		g.setComposite(ac);
		g.setBackground(Color.WHITE);
		// 在模板上添加用户二维码(地址,左边距,上边距,图片宽度,图片高度,未知)
		g.drawImage(imageCode, 419, 735, 124, 124, null);
		if (StringUtil.isNotEmpty(shareDesc)) {
			// 设置文本样式
			g.setFont(new Font("微软雅黑", Font.PLAIN, 40));
			g.setColor(Color.red);
			// 计算文字长度，计算居中的x点坐标
			g.drawString(shareDesc, imageLocal.getWidth() - 330, imageLocal.getHeight() - 530);

			// 设置文本样式
			g.setFont(new Font("微软雅黑", Font.PLAIN + Font.BOLD, 16));
			g.setColor(Color.WHITE);
			// 计算文字长度，计算居中的x点坐标
			String caewm = "长按二维码";
			g.drawString(caewm, 105, imageLocal.getHeight() - 10);
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		saveAsJPEG(imageLocal, out);
		out.close();
		//上传到七牛云并返回文件名称
		return QiNiuYun.uploadBytes("usershareimg",inviteCode+"_invitevip.jpg",out.toByteArray());
	}
	/**
	 * @Description : 获取贷款的海报
	 * @Param : originalImg 原图
	 * @Param : qrCodeLink 二维码链接地址
	 * @Param : shareDesc 图片文字
	 * @return : java.lang.String
	 */
	public static String getInviteLoansImg(String qrCodeLink,BufferedImage imageCode, String shareDesc,String inviteCode) throws Exception {
		// 加载原图图片
		BufferedImage temp = ImageIO.read(new URL(INVITE_LOANS_IMG));
		//解决图片失真的问题
		BufferedImage imageLocal = new BufferedImage(temp.getWidth(), temp.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
		imageLocal.getGraphics().drawImage(temp, 0, 0, null);
		// 生成二维码
//		BufferedImage imageCode = QrCodeUtil.createQrCodeBuffer(qrCodeLink, "temp.jpg");
		// 以原图片为模板
		Graphics2D g = imageLocal.createGraphics();
		AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
		g.setComposite(ac);
		g.setBackground(Color.WHITE);
		// 在模板上添加用户二维码(地址,左边距,上边距,图片宽度,图片高度,未知)
		g.drawImage(imageCode, 433, 752, 124, 124, null);
		if (StringUtil.isNotEmpty(shareDesc)) {
			// 设置文本样式
			g.setFont(new Font("微软雅黑", Font.PLAIN, 40));
			g.setColor(Color.red);
			// 计算文字长度，计算居中的x点坐标
			g.drawString(shareDesc, imageLocal.getWidth() - 330, imageLocal.getHeight() - 530);

			// 设置文本样式
			g.setFont(new Font("微软雅黑", Font.PLAIN + Font.BOLD, 16));
			g.setColor(Color.WHITE);
			// 计算文字长度，计算居中的x点坐标
			String caewm = "长按二维码";
			g.drawString(caewm, 105, imageLocal.getHeight() - 10);
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		saveAsJPEG(imageLocal, out);
		out.close();
		//上传到七牛云并返回文件名称
		return QiNiuYun.uploadBytes("usershareimg",inviteCode+"_inviteLoans.jpg",out.toByteArray());
	}

	
	/**
	 * 以JPEG编码保存图片
	 * @param fos           文件输出流
	 * @throws IOException
	 */
	private static void saveAsJPEG(BufferedImage imageToSave, ByteArrayOutputStream fos) throws IOException {
		ImageWriter imageWriter = ImageIO.getImageWritersBySuffix("jpg").next();
		ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
		imageWriter.setOutput(ios);
		if (jPEGcompression >= 0 && jPEGcompression <= 1f) {
			// new Compression
			JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
			jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
			jpegParams.setCompressionQuality(jPEGcompression);

		}
		ImageIO.setUseCache(false);
		imageWriter.write(new IIOImage(imageToSave, null, null));
		ios.close();
		imageWriter.dispose();
	}

	/**
	 * test
	 * 
	 * @param args @throws
	 */
	public static void main(String[] args) {
		long starttime = System.currentTimeMillis();
		System.out.println("开始：" + starttime);
		try {
			String qrCodeImg = "http://www.baidu.com";
			String shareDesc = "原价9999.99元";
//			String img = getInviteLoansImg(qrCodeImg, shareDesc,"2222222249");
//			System.out.println("生成完毕,url=" + img);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("结束：" + (System.currentTimeMillis() - starttime) / 1000);
	}

}
